package cn.tonyandmoney.voicenotes.alarm.broadcast

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.text.TextUtils
import cn.tonyandmoney.voicenotes.alarm.ui.AlarmActivity
import com.loonggg.lib.alarmmanager.clock.AlarmManagerUtil
import org.jetbrains.anko.AnkoLogger
import org.jetbrains.anko.info

class AlarmReceiver : BroadcastReceiver(), AnkoLogger {

    override fun onReceive(context: Context, intent: Intent) {
        val msg = intent.getStringExtra("msg")
        val intervalMillis = intent.getLongExtra("intervalMillis", 0)
        if (intervalMillis != 0L) {
            AlarmManagerUtil.setAlarmTime(context, System.currentTimeMillis() + intervalMillis, intent)
        }
        val flag = intent.getIntExtra("soundOrVibrator", 0)
        val id = intent.getIntExtra("id", -1)
        val voice = intent.getStringExtra("voice")
        info("onReceive: msg->$msg  flag->$flag  id->$id  voice: $voice")

        val clockIntent = Intent(context, AlarmActivity::class.java)
        clockIntent.putExtra("msg", msg)
        clockIntent.putExtra("flag", flag)
        clockIntent.putExtra("id", id)
        if (!TextUtils.isEmpty(voice))
            clockIntent.putExtra("voice", voice)
        clockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(clockIntent)
    }
}